//
//  T3FRouteManager.swift
//  t3_map
//
//  Created by Cunhai Li on 2024/12/11.
//

import Foundation
import Flutter
import CoreLocation

class T3FRouteManager {
    
    static var calculateLineTapWidth: CGFloat = 25
    
    /// 步行算路
    static func walkRoute(arguments: Any?, result: @escaping FlutterResult) {
        /// arguments示例：
        /// ```
        /// {
        ///    "mapType": 2
        ///    "origin": {
        ///         "longitude": 11.111
        ///         "latitude": 11.111
        ///    },
        ///    "destination": {
        ///         "longitude": 11.111
        ///         "latitude": 11.111
        ///    },
        /// }
        /// ```
        do {
            let json = try T3FJsonUtil.toJson(arguments)
            let mapType: T3FMapType
            if let value = json["mapType"] as? Int {
                mapType = T3FMapType(rawValue: value) ?? .amap
            }
            else {
                mapType = .amap
            }

            switch mapType {
            case .amap:
                T3FMARouteManager().walkRoute(arguments: arguments, result: result)
            case .tencentMap:
                T3FTNRouteManager().walkRoute(arguments: arguments, result: result)
            }
        } catch (let error) {
            let response = ["error": error.localizedDescription]
            let value = try? T3FJsonUtil.toString(response)
            result(value)
        }
    }

    /// 驾车算路
    static func driveRoute(arguments: Any?, result: @escaping FlutterResult) {
        /// arguments示例：
        /// ```
        /// {
        ///     "origin": {
        ///         "longitude": 11.111,
        ///         "latitude": 11.111
        ///    },
        ///    "destination": {
        ///         "longitude": 11.111,
        ///         "latitude": 11.111
        ///    },
        ///    "via": [
        ///         {
        ///             "longitude": 11.111,
        ///             "latitude": 11.111
        ///         },
        ///         {
        ///             "longitude": 11.111,
        ///             "latitude": 11.111
        ///         }
        ///    ],
        ///    "originPoiId": "abcd",
        ///    "destPoiId": "abcd",
        ///    "departureTime": 123456, //时间戳
        /// }
        /// ```
        do {
            let json = try T3FJsonUtil.toJson(arguments)
            let mapType: T3FMapType
            if let value = json["mapType"] as? Int {
                mapType = T3FMapType(rawValue: value) ?? .amap
            }
            else {
                mapType = .amap
            }

            switch mapType {
            case .amap:
                T3FMARouteManager().driveRoute(arguments: arguments, result: result)
            case .tencentMap:
                T3FTNRouteManager().driveRoute(arguments: arguments, result: result)
            }
        } catch (let error) {
            let response = ["error": error.localizedDescription]
            let value = try? T3FJsonUtil.toString(response)
            result(value)
        }
    }
}
